Repository navigation
Qwen3VL: default per-image resolution to a 1,280 vision-token budget - #398
Conversation
|
Lint fixed. |
…udget The qwen3_5 ViT runs global O(patches^2) attention with no window structure, so an uncapped full-resolution image (PARO ships a ~16 MP longest_edge that never clamps) can allocate a tens-of-GB attention score matrix. Default each image -- and each video frame -- to a 2,560 vision-token budget (min(config.maxPixels, 2560 * factor^2)), mirroring the Qwen2.5-VL clamp, and honor an explicit processing.maxPixels override. Downscales only what the tower sees; the stored bytes and image digest are untouched. The default budget multiply (2560 * factor^2) is guarded with multipliedReportingOverflow so a pathological patch/merge size degrades to the config ceiling instead of trapping, and both the image and video preprocess paths clamp through config.maxPixels / config.minPixels so the two clamp sites cannot drift. See tesseract ADR-0014. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
3ad5fbe to
c424edd
Compare
| /// Pixels for the default 2,560 vision-token budget (`2560 * factor²`), | ||
| /// clamped to `ceiling`. Overflow-safe: a pathological config (an absurd | ||
| /// `patch_size`/`merge_size`) yields `ceiling` instead of trapping on the | ||
| /// unchecked multiply, so bad model metadata degrades to the config ceiling | ||
| /// rather than crashing preprocess (tesseract ADR-0014). |
There was a problem hiding this comment.
Question on the 2560 value -- is this too large? It looks like the mlx-vlm side uses 1280:
processing_qwen3_vl.py
def _smart_resize_image(
...
max_pixels: int = 14 * 14 * 4 * 1280,as does the 25VL in swift:
Qwen25VL.swift
// Qwen2.5-VL ships a very large max_pixels; default to the budget the
// model card recommends (1280 * 28 * 28), override-able via `processing`.
let maxPixels = processing?.maxPixels ?? min(config.size.maxPixels, 1280 * factor * factor)The code looks good but I am curious about the number.
There was a problem hiding this comment.
You're right, and it turns out 1280 is even a touch generous. The mlx-vlm constant (14 * 14 * 4 * 1280) carries patch-14 geometry from the qwen2 lineage, while qwen3_vl is patch 16, so on this model it works out to roughly 1.0 MP, about a 980-token budget. Qwen25VL.swift is the same pixel count (1280 * 28²). 1280 * factor² here lands at 1.31 MP, slightly above both precedents, with the same expression as the 25VL clamp.
Before changing it I ran the halved budget against real workloads in our app. Compared with 2560 the extractions come out the same. The one caveat I found is on a 5 MP Hacker News screenshot, where two usernames in the small gray byline text came back off by one glyph (bilsbie read as bilskie, SirOibaf as SirOlbaf). Titles, domains, points, timestamps and comment counts all survived intact. That seems like the right trade for a default, since processing.maxPixels stays available when someone needs full fidelity on large dense images.
Default is now 1280 (637e9f2), title and description updated.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Matches the Qwen25VL default expression; tested on dense-text screenshots with no information loss vs 2,560. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
1,280 keeps showing up across these defaults for a reason: it's the resolution range the model was tuned in. The cap isn't really a compute limit — it's a distribution limit. Go far past it and the extra pixels aren't detail the model can use; position statistics drift out of the range it knows, and box geometry goes first. I measured this on Qwen2.5-VL-7B-4bit over ScreenSpot-Pro screenshots. Large targets (>5% of the frame, n=36) peak exactly at 1,280 — 0.643 IoU, down to 0.526 by 16,384. More budget makes the common case worse, not better. Your off-by-one glyphs sit on the other curve. Small targets (0.1–1%) keep gaining up to ~4,096 (0.111 → 0.179), and tiny text is still climbing at 16,384. So the default lands right, and ~4,096 is a sensible override for dense screenshots. Full size-binned table + method: https://dev.to/nivdvir/maxpixels-is-a-token-budget-in-disguise-and-the-right-cap-depends-on-the-size-of-what-youre-l0c |
|
@NivDvir great info thank you! |
…l-explore#398) feat(qwen3vl): cap per-image/frame vision-token budget at 1,280 (min(config.maxPixels, 1280*factor²), overflow-safe), matching Qwen2.5-VL clamp to avoid unbounded O(patches²) attention blowup on full-res images. See ADR-0014.
…akeAlong M-RoPE (#127) Three ports from upstream mlx-swift-lm onto the fork, targeting vision-request cost on qwen3.5/3.6-VL and qwen3-vl-30b at B=1-5: 1. Per-image fused SDPA (upstream #455): the vision tower attention built a dense [1, L, L] -1e9 mask over the concatenated patch sequence and fell off the fused SDPA kernel (head dim 72), materializing O(L^2) score matrices. Attention is block-diagonal over cuSeqlens, so attend each image's segment independently and zero-pad Q/K/V 72 -> 80 to dispatch the fused kernel (exact math; scale passed explicitly). Upstream measured 28.7 -> 12.5 GB peak and 59.8 -> 36.3 s prefill on a single large image. 2. Default image vision-token budget (upstream #398): images previously rode the checkpoint's max_pixels (12.8 MP on served configs) straight into the global O(patches^2) ViT. Default each image to a 1,280 vision-token budget (1280 * factor^2 pixels), overflow-safe, override-able per request via UserInput.Processing.minPixels/maxPixels — the same philosophy as the existing 512^2 video-frame clamp, which stays the video ceiling (an explicit processing.maxPixels can narrow but not exceed it). BEHAVIOR CHANGE: large images now downscale by default. 3. Interleaved M-RoPE via precomputed takeAlong (upstream #442): all three M-RoPE sites (MLXVLM Qwen35 + Qwen3VL RotaryEmbedding, and the fork's MLXLLM Qwen35MRoPE default path) built the t/h/w interleave with a per-frequency slice loop — ~dims lazy ops per attention layer per forward (~3,000 tiny ops per decode token on qwen3-vl-30b). Precompute the plane index per frequency at init and select with one takeAlong. The MLXLLM fallback (non-default rope scaling) path is unchanged. Tests: slice-loop-reference parity for all three M-RoPE sites, dense-mask parity and pad-to-80 exactness for the vision attention, and budget-resolution unit coverage. Not included (adjacent known issue): the image preprocess path still skips the sRGB tone curve that the video path applies.
…d-rows-donate fix(cbv2): rows that cannot speculate save prefix checkpoints (ml-explore#397 + Qwen 3.5 production-shape tests)
…ed-row-reads SDK main now carries ml-explore#398 (non-speculating rows save prefix checkpoints), so a pin of this branch must contain it. No file in common: ml-explore#398 changes the Qwen MTP capture and EngineLoopV2's donation eligibility; this branch changes only the lossy export's row reads and coding. Qwen checkpoints with the MTP head stay native (CheckpointStorageQuantization), so ml-explore#398's new donors never take the coded path this branch changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d-buffers No conflicts: ml-explore#398 touches none of ml-explore#378's files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>






Fixes #396.
The qwen3_5 ViT runs global O(patches²) attention with no window structure, so an uncapped full-resolution image (several published configs ship a ~16 MP longest_edge that never clamps) can allocate a tens-of-GB attention score matrix. Measured on a 27B Qwen3.6 checkpoint: 47 GB peak / 121 s prefill for one full-screen screenshot; ~21 GB with a 2,560-token cap, lower still at the current default.
min(config.maxPixels, 1280 * factor²), mirroring the Qwen2.5-VL clamp.processing.maxPixelsoverride, so full resolution stays available as an opt-in.multipliedReportingOverflowso a pathological patch/merge size degrades to the config ceiling instead of trapping, and the image and video preprocess paths clamp through the sameconfig.maxPixels/config.minPixelsaccessors so the two clamp sites cannot drift.